package com.UnionPayCenter.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.UnionPayCenter.entity.OperationScenes;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IOperationScenesService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: UnionPayCenter
 * @description:
 * @author BaoJF
 *
 */
@Slf4j
@Api(value = "operScenes", tags = "运营场景")
@RestController
@RequestMapping("/operScenes")
public class OperationScenesController {
    @Autowired
    private IOperationScenesService operScenesService;

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "运营场景", httpMethod = "POST", value = "[后台]查询运营场景列表", response = OperationScenes.class)
    public ResponseWrapper findList(
            @ApiJsonObject(name = "operScenesFindListParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "type", value = "场景类型", type = "int", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            PageInfo<OperationScenes> list = operScenesService.findList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询运营场景列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询运营场景列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(tags = "运营场景", httpMethod = "POST", value = "[后台]添加运营场景", response = String.class)
    public ResponseWrapper add(
            @ApiJsonObject(name = "operScenesAddParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "opsName", value = "场景名称"),
                    @ApiJsonProperty(key = "type", value = "场景类型(1-注册成功;2-充值成功)", type = "int"),
                    @ApiJsonProperty(key = "amount", value = "充值满送金额", type = "int", required = false),
                    @ApiJsonProperty(key = "couponList", value = "优惠券list")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            String opsName = jsonParam.getString("opsName");
            Integer type = jsonParam.getInteger("type");
            String couponList = jsonParam.getString("couponList");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (StringUtils.isNullStr(opsName)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "opsName"));
            }
            if (type == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "type"));
            }
            if (StringUtils.isNullStr(couponList)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "couponList"));
            }
            operScenesService.add(jsonParam);
            return ResponseWrapper.succeed("添加成功");
        }catch (ApiException e){
            log.error(String.format("[后台]添加运营场景失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]添加运营场景失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(tags = "运营场景", httpMethod = "POST", value = "[后台]修改运营场景", response = String.class)
    public ResponseWrapper update(
            @ApiJsonObject(name = "operScenesUpdateParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "opsId", value = "场景id", type = "int"),
                    @ApiJsonProperty(key = "opsName", value = "场景名称"),
                    @ApiJsonProperty(key = "type", value = "场景类型(1-注册成功;2-充值成功)", type = "int"),
                    @ApiJsonProperty(key = "amount", value = "充值满送金额", type = "int", required = false),
                    @ApiJsonProperty(key = "couponList", value = "优惠券list")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            String opsName = jsonParam.getString("opsName");
            Integer type = jsonParam.getInteger("type");
            String couponList = jsonParam.getString("couponList");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (StringUtils.isNullStr(opsName)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "opsName"));
            }
            if (type == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "type"));
            }
            if (StringUtils.isNullStr(couponList)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "couponList"));
            }
            operScenesService.update(jsonParam);
            return ResponseWrapper.succeed("修改成功");
        }catch (ApiException e){
            log.error(String.format("[后台]修改运营场景失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]修改运营场景失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ApiOperation(tags = "运营场景", httpMethod = "POST", value = "[后台]启/停用运营场景", response = String.class)
    public ResponseWrapper updateStatus(
            @ApiJsonObject(name = "updateStatusParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "opsId", value = "场景id", type = "int"),
                    @ApiJsonProperty(key = "status", value = "启停状态(0-启用;1-停用)", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            Integer opsId = jsonParam.getInteger("opsId");
            Integer status = jsonParam.getInteger("status");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (opsId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "opsId"));
            }
            if (status == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "status"));
            }
            operScenesService.updateStatus(jsonParam);
            return ResponseWrapper.succeed("操作成功");
        }catch (ApiException e){
            log.error(String.format("[后台]启/停用运营场景失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]启/停用运营场景失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/findOperInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "运营场景", httpMethod = "POST", value = "[后台]运营场景详情查询", response = Map.class)
    public ResponseWrapper findOperInfo(
            @ApiJsonObject(name = "findOperInfoParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "opsId", value = "场景id", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            Integer opsId = jsonParam.getInteger("opsId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (opsId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "opsId"));
            }
            Map<String, Object> result = operScenesService.findOperInfo(jsonParam);
            return ResponseWrapper.succeed(result);
        }catch (ApiException e){
            log.error(String.format("[后台]运营场景详情查询失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]运营场景详情查询失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
